package org.nhindirect.stagent;

import java.io.IOException;
import java.io.Writer;

public class EntityWriter 
{
	Writer m_writer;
    
    public EntityWriter(Writer writer)
    {
        this.setWriter(writer);
    }
    
    public void setWriter(Writer writer)
    {
        if (writer == null)
        {
            throw new IllegalArgumentException();
        }
        this.close();
        this.m_writer = writer;
    }

    public void write(HeaderCollection headers)
    {
        if (headers == null)
        {
            throw new IllegalArgumentException();
        }
        
        for (Header header : headers)
        {
            this.write(header);
        }
    }
    
    public void write(Header header)
    {
        if (header == null)
        {
            throw new IllegalArgumentException();
        }
        
        this.writeLine(header.getSourceText());
    }
    
    public void write(Body body)
    {
        if (body == null)
        {
            throw new IllegalArgumentException();
        }
        
        this.write(body.getSourceText());
    }
    
    public void writeHeader(String name, String value)
    {
        if (name == null || name.length() == 0)
        {
            throw new IllegalArgumentException();
        }
        
        try
        {
	        this.m_writer.write(name);
	        if (value != null && value.length() > 0)
	        {
	            this.m_writer.write(Protocol.NameValueSeparator);
	            this.m_writer.write(value);
	        }
        }
        catch (IOException e){}
        this.writeCRLF();
    }
            
    public void writeMimeBoundary(String boundary, boolean isLast)
    {
        //
        // As per MIME spec, boundaries START with a CRLF
        //
        this.writeCRLF();
        if (isLast)
        {
            this.writeLine(Protocol.BoundarySeparator + boundary + Protocol.BoundarySeparator);
        }
        else
        {
            this.writeLine(Protocol.BoundarySeparator + boundary);
        }
    }
    
    public void write(StringSegment text)
    {
        for(int i = 0, count = text.getLength(); i < count; ++i)
        {
        	try
        	{
        		this.m_writer.write(text.charAt(i));
        	}
        	catch (IOException e){}
        }
    }
            
    public void writeLine(StringSegment text)
    {
        this.write(text);
        this.writeCRLF();
        
    }
    
    public void writeLine(String text)
    {
    	try
    	{
    		this.m_writer.write(text);
    	}
    	catch (IOException e){}
        this.writeCRLF();
    }
    
    public void writeCRLF()
    {
    	try
    	{
    		this.m_writer.write(Protocol.CRLF);
    	}
    	catch (IOException e){}
    }        
    
    public void close()
    {
        if (this.m_writer != null)
        {
            try
            {
            	this.m_writer.close();
            }
        	catch (IOException e){}
            this.m_writer = null;
        }        
    }
    
    public void flush()
    {
    	if (this.m_writer != null)
    	{
    		try
    		{
    			this.m_writer.flush();
    		}
    		catch (IOException e){}
    	}
    }
    

    public void finalize()
    {
        this.close();
    }
}
